file(GLOB JS_FILES_ CONFIGURE_DEPENDS "*.js")

add_custom_command(
    OUTPUT ${CMAKE_BINARY_DIR}/jscode.c
    DEPENDS ${JS_FILES_} qjsc
    COMMAND echo "generate file: " ${CMAKE_BINARY_DIR}/jscode.c
    COMMAND ${EXECUTABLE_OUTPUT_PATH}qjsc -m -c -o ${CMAKE_BINARY_DIR}/jscode.c ${JS_FILES_}
)
# 用来展示所有的js代码 空的target
add_custom_target(js SOURCES ${JS_FILES_})

# 生成jscode库用来引入所有的js c模块
set(module_load_code "")
set(module_load_extern "")
foreach(name ${JS_FILES_})
    get_filename_component(jse_name ${name} NAME_WE)
    if(NOT ${jse_name} STREQUAL "")
        set(module_load_extern "${module_load_extern}extern const uint8_t qjsc_${jse_name}[];\nextern const uint32_t qjsc_${jse_name}_size;\n\n")
        set(module_load_code "${module_load_code}  js_std_eval_binary(ctx, qjsc_${jse_name}, qjsc_${jse_name}_size, 0);\n")
    endif(NOT ${jse_name} STREQUAL "")
endforeach(name)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/module_init.c.in ${CMAKE_BINARY_DIR}/module_init.c @ONLY)
# 生成jscode库用来链接所有的内嵌js代码
add_library(qjscode EXCLUDE_FROM_ALL 
    ${CMAKE_BINARY_DIR}/jscode.c 
    module_init.h
    ${CMAKE_BINARY_DIR}/module_init.c
)
target_include_directories(qjscode 
    PRIVATE ${CMAKE_SOURCE_DIR}/third_party/quickjspp/quickjs
    INTERFACE ${CMAKE_SOURCE_DIR}/src/jse/js
)
target_link_libraries(qjscode quickjs)